var $ = require("jquery");
var pop = require("pop/pop.js");
var store=require("store");

var def_opt = {
    cache : false,
    dataType : "json"
};
var ajax = function(opt){
	opt.contentType=opt.contentType || "application/json";
	var _success = opt.success;
    opt.success = function(rs) {
    	var errorCode = rs.errorCode;
        /*if ((errorCode=="1001" || errorCode=="1002") && !~location.href.indexOf("/webapp/html/login.html")) {
    		window.location.href = "/webapp/html/login.html";
    		return;
        }*/
    	var args = [].slice.call(arguments,0);
    	_success && _success.apply(opt.context, args);
    }
    opt = $.extend({},def_opt , opt );
    return $.ajax({
        url:opt.url,
        type:opt.type,
        dataType:'json',
        data:{
            "data" : JSON.stringify(opt.data)
        },
        success:function(res){
        	var errorCode = res.errorCode;
        	if (errorCode=="1001" || errorCode=="1002") {
        		pop.tip(res.msg);
	    		window.location.href="/login";
	    		return;
	        }
        }
        
    });
};

var objToString = function(obj){
    var flag = false;
    var str = '{';
    for(i in obj) {
        flag = true;
        str +='"'+i +'":';
        if(obj[i]  instanceof Array){
            str += '[';
            var arrs = obj[i];
            for( var j = 0;j < arrs.length;j++){
            	if(arrs[j]){
                	str +='"'+arrs[j]+'"';
               	}else{
               		str += arrs[j];	
               	}
                if(j <  arrs.length -1){
                    str += ',';
                }
            }
            str+=']';
        }else if(typeof  obj[i]  == 'object'){
            str +=objToString(obj[i]);
        }else if(typeof  obj[i]  == 'number'){
        	str +=obj[i];
        }else{
            str +='"'+obj[i]+'"';
        }
        str +=',';
    }
    if( flag == true){
        str = str.substring(0,str.length - 1);
    }
    str +='}';
    return str;

}



//生成验签值
var signFun = function(obj){
	var userInfo=store.get();
	var _userData = userInfo.nbuser.a;
	var _appKey;
	if(_userData){
		if(_userData.name1){
			_appKey = $.base64.decode(_userData.name1);
		}else{
			_appKey = "";
		}
		
	}
    var str = objToString(obj) + ','+_appKey;
    return $.md5(str);
}
//生成验签值
var signFun2 = function(obj){
	var userInfo=store.get();
	var _userData = userInfo.nbuser.a;
	var _appKey;
	if(_userData){
		if(_userData.name1){
			_appKey = $.base64.decode(_userData.name1);
		}else{
			_appKey = "";
		}
		
	}
    var str = JSON.stringify(obj) + ','+_appKey;
    return $.md5(str);
}

var http = {
	
    get : function(opt){
        opt.type = "get";
        opt.data.sign = signFun(opt.data.param);
        opt.data = JSON.stringify({
        	"data" : opt.data
        });
        var data = opt.data || {};
        return ajax(opt);
    },
    post : function(opt){
        var paramWithSign = $.extend(true,{},opt.data);
        delete paramWithSign.param;
        paramWithSign.param = opt.data.param;
        var sign = signFun(opt.data.param);
        paramWithSign.sign = sign;
        
	    opt.type = "post";
        opt.data = paramWithSign;
        var data = opt.data || {};
        return ajax(opt);
    },
    post2 : function(opt){
        var paramWithSign = $.extend(true,{},opt.data);
        delete paramWithSign.param;
        paramWithSign.param = opt.data.param;
        var sign = signFun2(opt.data.param);
        paramWithSign.sign = sign;
        
	    opt.type = "post";
        opt.data = paramWithSign;
        var data = opt.data || {};
        return ajax(opt);
    },
    form_post : function(opt){
        var paramWithSign = $.extend(true,{},opt.data);
	    opt.type = "post";
        var data = opt.data || {};
        return ajax(opt);
    }
    
    
};

module.exports = http;
